<?php  
    //登录
	/*
	//包含数据库连接文件
	include('conn.php');
	//检测用户名及密码是否正确
	$check_query = mysql_query("select uid from user where username='$username' and password='$password' limit 1");
	if($result = mysql_fetch_array($check_query)){
		//登录成功
		$_SESSION['username'] = $username;
		$_SESSION['userid'] = $result['uid'];
		echo $username,' 欢迎你！进入 <a href="my.php">用户中心</a><br />';
		echo '点击此处 <a href="login.php?action=logout">注销</a> 登录！<br />';
		exit;
	} else {
		exit('登录失败！点击此处 <a href="javascript:history.back(-1);">返回</a> 重试');
	}
	*/
	
	include('./db/dbconn.php');
	
	
	if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        $user = $_POST["username"];
		$email = $_POST["email"];
        $psw = $_POST["password"];
		$psw_rep = $_POST["password_rep"];
		$type = 1;
		
		if ($psw != $psw_rep) {
			$json_err = array("status" => "fail", "errors" => array("password" => ["两次密码不一致"], "password_rep" => ["两次密码不一致"]));
			echo json_encode($json_err);
		} else {
			$sql = "select uid from user where uid='$user' limit 1";
			$check_query = mysqli_query($conn, $sql);
			if ($result = mysqli_fetch_array($check_query)) {
				$json_err = array("status" => "fail", "errors" => array("username" => ["用户名已存在"]));
				echo json_encode($json_err);
			} else {
				$sql = "insert into user values('$user', '$psw', '$email', '$type')";
				mysqli_query($conn, $sql);
				
				//登录成功
				$_SESSION['user'] = $user;
				$expiry = 3600;
				setcookie("user", $user, time() + $expiry);
				
				$json_err = array("status" => "success");
				echo json_encode($json_err);
			}
		}
		
    }
	/*
	if (isset($_POST["submit"])) {
		
	}
	*/
	/*	
	if (isset($_POST["submit"])) {
        $user = $_POST["username"];
        $psw = $_POST["password"];
		$expiry = $_POST["session_expiry"];
		$json = array("status" => "success");
		echo json_encode($json);
		//echo "<script>alert('" . json_encode($json) . "!');</script>";
	}
	*/
	/*
	if (isset($_POST["submit"])) {
        $user = $_POST["username"];
        $psw = $_POST["password"];
		$expiry = $_POST["session_expiry"];
		// echo "<script>alert('" . $user . " " . $psw . "!');</script>"; 
        if ($user == "" || $psw == "") {
            echo "<script>alert('请输入用户名或密码!'); history.go(-1);</script>";
        } else {
			if ($user == "admin" && $psw == "admin") {
				$json = array("status" => "success");
				$jsondata = json_encode($json);
				//echo $jsondata;
				echo "<script>alert('" . $jsondata . "!');</script>";  
			}
			
			mysql_connect("localhost","root","sixx");  
            mysql_select_db("vt");  
            mysql_query("set names 'gbk'");  
            $sql = "select username,password from user where username = '$_POST[username]' and password = '$_POST[password]'";  
            $result = mysql_query($sql);  
            $num = mysql_num_rows($result);  
            if($num)  
            {  
                $row = mysql_fetch_array($result);  ////将数据以索引方式储存在数组中
                echo $row[0];  
            }  
            else
            {  
                echo "<script>alert('用户名或密码不正确!');history.go(-1);</script>";  
            }
			
        }
    } else {
        echo "<script>alert('提交未成功'); history.go(-1);</script>";
    }
	*/
	
?>  